# -*- coding: utf-8 -*-
import sys
import os
import time
from datetime import datetime
import sumc
from sumc import client
from sumc import data


import logging as l
l.basicConfig(stream=sys.stdout,
              format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
              level=l.INFO)
hdlr = l.FileHandler('scrape.log')
hdlr.setLevel(l.INFO)
hdlr.setFormatter(l.Formatter("%(asctime)s %(name)s %(levelname)s - %(message)s"))
l.getLogger().addHandler(hdlr)



dbFile = 'sumc.db'
if os.path.isfile(dbFile):
    os.unlink(dbFile)

db = data.DBHelper(dbFile)
db.createDatabase()
c = client.ClientByLine()

started = time.time()

l.info("Scrape started")


for ttype in sumc.TRANSPORT_TYPES:
    
    lines = c.getLines(ttype)
    
    for remoteLineId, lineLabel in lines:
        
        l.info(" Getting data for (type, line id) %s: %s: label '%s'", ttype, remoteLineId, lineLabel)
        routes = c.getLineRoutes(ttype, remoteLineId)
        l.info(" Line %s.%s; routes: %s, %s", ttype, lineLabel, routes[0][0], routes[1][0])

        lineId = db.addLine(remoteLineId, ttype, lineLabel, routes[0], routes[1])
        for (routeId, _) in routes:
            stops = c.getRouteStops(ttype, remoteLineId, routeId)
            l.info("    Route %s, Found %s stops", routeId, len(stops))
            for remoteStopId, stopLabel in stops:
                stopCode = sumc.getStopCodeFromLabel(stopLabel)
                stopId = db.findStopByCode(stopCode)
                if not stopId:
                    stopId = db.addStop(stopLabel)
                    
                db.addLineStopLink(lineId, stopId, routeId, remoteStopId)

finished = time.time()

elapsed = finished - started
l.info("Scrapping took %s seconds", elapsed)